//app.js
const apisj = require('./api/apisj')
const api = require('./api/api')
const QQMapWX = require('./utils/qqmap-wx.js');
const Promise = require('./utils/bluebird.min.js');
var distance = new QQMapWX({
    key: 'DN6BZ-YKMRP-EUHDE-LEBIO-YR736-SRBUI' // 必填
});

App({

    onLaunch() {
        // 展示本地存储能力
        var that = this
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId

                api.request("/api/Account/GetTokenForWeChat", { code: res.code }, "POST", (res) => {
                    wx.setStorageSync("TOKEN", res.data.obj.token)
                    wx.setStorageSync("DRIVERS", res.data.obj.drivers)
                    wx.setStorageSync("COMPANY", res.data.obj.company)
                    getApp().globalData.token = res.data.obj.token
                    getApp().globalData.company = res.data.obj.company
                    if (res.data.responseCode == 1000) {
                        api.tokenL = res.data.obj.token
                        api.drivers = res.data.obj.drivers
                        api.company = res.data.obj.company
                        apisj.tokenL = res.data.obj.token
                        apisj.drivers = res.data.obj.drivers
                        apisj.company = res.data.obj.company
                        this.location()
                    }
                    if (this.getUserOpenidCallback) {
                        this.getUserOpenidCallback(res)
                    }
                }, (res) => {})
            }
        })
        var that = this
        setInterval(function() {
                that.location()
            }, 300000)
            // setTimeout
            // await this.login()

    },
    location() {
        distance.reverseGeocoder({
            success: function(res) {
                wx.setStorageSync("latitude", res.result.location.lat)
                wx.setStorageSync("longitude", res.result.location.lng)
                wx.setStorageSync("address", res.result.address)
                api.request("/api/VehicleLocation/Update", { driver: { id: api.drivers.id, name: api.drivers.name, tel: api.drivers.tel, }, longitude: res.result.location.lng, latitude: res.result.location.lat, locationAddress: res.result.address }, "POST", (data) => {}, (res) => {})
            },
            fail: function(res) {},
            complete: function(res) {}
        });
    },
    getCode: function (c) {
      var wxCurrPages = getCurrentPages();
      wxCurrPages[wxCurrPages.length - 1].setCode(c)
    },
    globalData: {
        userInfo: null,
        driversOrders: [],

        code2:[],
        token:'',
        id:'',
        orderid:'',
        //apiServer:'https://clientapitest.dwilms.com/',//公司测试
        apiServer: 'https://clientapi.dwilms.com/',//公司正式

        //apiServer:'http://clientapi.dwilms.com:92',
       // apiServer:'https://clientapitest.dwilms.com/',
      company:'',
        items:[],
        scanCodeList: [], //已扫码的列表明细
        scanGoodsList: [], //待扫描货物列表
        scanCode: "",//扫描的二维码
        scanCodesList:[],//公共扫码
        deviceId:"",//仪器id
        isLink:false //蓝牙是否连接
    },
    getApi: api,
    getApisj: apisj
})